define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        Monkey = require("./monkey"),
        config = app.config,
        method = app.method;
        require("./jq_pagination");        

        if($("#select-goods").length){  

            if($("#goodPrice").length){
                $("#bottomLine").attr("data-rule-max",$("#goodPrice").val());
            }

            var SelecteFun = function(opts) {
                this.selectObj = window.goods_set;
                this.selectedArray = JSON.parse($("#beenSelected").val());
                this.pageIndex = 0;
                this.globalBoll = true;
                this.selectThat = [];
                this.opts = $.extend(true,{
                    firstUrl:"/Plus/prefer1.php",
                    partUrl:"/Plus/prefer.php",
                    listUrl:""
                },opts||{});
                this.init();
            }

            SelecteFun.prototype = {
                init:function(){
                    var _this = this;
                    _this.create({});
                    _this._click();
                    _this.searchF();
                    _this.saveD();
                    _this.hide();

                    if(_this.selectedArray.length){
                        var html = template("js_template",this.selectObj)
                        $("#selected-goods-box").append(html);
                        $("#selected-goods-box").removeClass("hide");
                        $("#select-goods-box").addClass("hide");
                        $("#bottomLine").data("ruleMax",this.selectObj.price);
                        $("#selected-goods-box").find(".remove-box").remove();
                    }else{
                        $("#selected-goods-box").addClass("hide");
                        $("#select-goods-box").removeClass("hide");
                    }

                    $("#select-goods").on("click",function(){
                        $("#js_question_modal").modal("show");
                        _this.globalBoll = true;
                        _this.removeObj(true);
                    });

                    $(document).on("change","#classification",function(){
                        $("#all-list-box").empty();
                        _this.create($("#commodity-box").serialize());
                    })
                },
                create:function(searchData){
                    var _this = this;
                    Monkey.post(_this.opts.firstUrl,function(d){
                        if(d){
                            var $html = template("all_template",d);
                            $('#all-list-box').append($html);
                            var initPagination = function() {
                                // 创建分页
                                $("#Pagination").pagination(d.numberPages, {
                                    num_edge_entries: 1, //边缘页数
                                    num_display_entries: 4, //主体页数
                                    callback: pageselectCallback,
                                    items_per_page: 1, //每页显示1项
                                    prev_text: "&laquo",
                                    next_text: "&raquo",
                                    load_first_page: true //第一次自动加载,true(默认),false(不加载)
                                });
                             };

                            var pageselectCallback = function(page_index, jq){

                                $('#all-list-box div.list-box').hide();
                                if(!$('#all-list-box div.list-box:eq(' + page_index + ')').data("crate")){
                                    var data = $("#commodity-box").serialize();
                                    data += "&page=" + (page_index+1) + "";
                                    Monkey.post(_this.opts.listUrl,function(datas){
                                        var _html = template("list_template",datas);
                                        $('#all-list-box').empty();
                                        $('#all-list-box').append(_html);
                                        _this.removeObj(false);
                                    },'',null,data)
                                }
                                $('#all-list-box div.list-box:eq(' + page_index + ')').show();
                                
                                //阻止单击事件
                                return false;
                            }

                            initPagination();
                        }

                    },'',null,searchData);
                },
                _click:function(){
                    var _this = this;
                    $(document).on("click","li.itmes",function(){
                        var cid = $(this).data("cid");
                        var activeItme = $(this).find("a");
                            
                        $(this).siblings("li.itmes").removeClass("active");
                        $(this).siblings("li.itmes").find("a").removeClass("active");

                        activeItme.toggleClass("active");
                        $(this).toggleClass("active");

                        _this.selectedArray = [];
                        _this.selectThat = [];

                        if($(this).hasClass("active")){
                            _this.selectedArray.push(cid);
                            _this.selectThat.push($(this));
                        }else{
                            _this.selectedArray = [];
                            _this.selectThat = [];
                        }

                    })
                },
                searchF:function(){
                    var _this = this;
                    $(document).on("click","#search",function(){
                        $("#all-list-box").empty();
                        _this.create($("#commodity-box").serialize());
                    })
                },
                saveD:function(){
                    var _this = this;
                    $(document).on("click","#saveData",function(){
                        if(_this.selectedArray.length){
                            $("#beenSelected").val("["+_this.selectedArray+"]");
                        }else{
                            $("#beenSelected").val("[]");
                        }

                        if(_this.selectThat.length){
                            $("#goodPrice").val(_this.selectThat[0].data("price"));
                        }else{
                            $("#goodPrice").val("");
                        }

                        if($("#bottomLine").length){
                            var $max = parseFloat($("#goodPrice").val());
                            var _html = '<input type="text" class="form-control" id="bottomLine" name="bottomLine" data-rule-required="true" data-rule-ismoney="true" value="" data-rule-max="'+$max+'" data-msg-max="砍价底线价格须小于等于商品价格" />';
                           $("#bottomLine").closest("div").empty().append(_html);
                        }

                        if(_this.selectThat.length){
                            _this.selectObj.cid = _this.selectThat[0].data("cid");
                            _this.selectObj.imgsrc = _this.selectThat[0].data("imgsrc");
                            _this.selectObj.goodsName = _this.selectThat[0].data("text");
                            _this.selectObj.inventory = _this.selectThat[0].data("inventory");
                            _this.selectObj.price = _this.selectThat[0].data("price");
                                
                            $("#selected-goods-box").empty();
                            var html = template("js_template",_this.selectObj);
                            $("#selected-goods-box").append(html);
                            $("#selected-goods-box").removeClass("hide");
                        }else{
                            $("#selected-goods-box").empty();
                        }

                        _this.globalBoll = false;

                    });
                },
                hide:function(){
                    var _this = this;
                    $('#js_question_modal').on('hidden.bs.modal', function (e) {
                        _this.globalBoll && _this.removeObj(true);
                    })
                },
                removeObj:function(bolg){

                    var _this = this;
                    var selectedCid = [];

                    if(bolg){
                        selectedCid = JSON.parse(""+$("#beenSelected").val()+""); 
                    }else{
                        selectedCid = _this.selectedArray;
                    }

                    $('#all-list-box div.list-box:eq(' + _this.pageIndex + ')').find("li.itmes").each(function(){
                        $(this).removeClass("active");
                        $(this).find("a").removeClass("active");
                        for(var k=0;k<selectedCid.length;k++){
                            if(selectedCid[k] == $(this).data("cid")){
                                $(this).addClass("active");
                                $(this).find("a").addClass("active");
                            }
                        }
                    });

                    if(bolg){
                        _this.selectedArray = JSON.parse(""+$("#beenSelected").val()+"");
                    }
                    
                }
            }

            if(window.config){
                new SelecteFun({
                    firstUrl:window.config.firstdata,
                    listUrl:window.config.listdata
                });
            }

            $("#submit-btn").click(function(){
                if(!JSON.parse($("#beenSelected").val()).length){
                    config.msg.info("请添加活动商品");
                    return false;
                }
            })
        }

        if($("input.js_every_time").length){
            $("input.js_every_time").click(function(){
                var data = $(this).data("val");
                if(data){
                    $(".open1").find("input[type='text']").removeAttr("disabled");
                    $(".open2").find("input[type='text']").removeClass("error");
                    $(".open2").find("input[type='text']").attr("disabled","disabled");
                    $(".open2").find("span.help-block").remove();
                }else{
                    $(".open2").find("input[type='text']").removeAttr("disabled");
                    $(".open1").find("input[type='text']").removeClass("error");
                    $(".open1").find("input[type='text']").attr("disabled","disabled");
                    $(".open1").find("span.help-block").remove();
                }
                
            })

            $("input.js_change_radio").click(function(){
                var show = $(this).data("show");
                if(show){
                    $(".js_hide_box").removeClass("hide");
                }else{
                    $(".js_hide_box").addClass("hide");
                }
            })
        }
})